<?php
namespace app\index\controller;

class Index extends Base
{
    public function index()
    {
        /*
         * 从数据库中查询数据,把数据分配
         */

        //读取当前用户的的id,储存在session中
        $uid = session('userLoginInfo')['id'];

        //操作Events模型,获取所有事件
        $data = \app\index\model\Events::where('uid',$uid)
            ->where('isdelete','<>','1')
            ->order('create_time DESC')
            ->paginate(5);  //分页使用,每一页显示3条


//        print_r($data);

        //获取正在进行中的事件
        $doing = \app\index\model\Events::where('uid',$uid)
            ->where('isdelete','<>','1')
            ->where('status','=','0')
            ->order('create_time DESC')
            ->paginate(5);



        //获取已完成的事件
        $done = \app\index\model\Events::where('uid',$uid)
            ->where('isdelete','<>','1')
            ->where('status','=','1')
            ->order('create_time DESC')
            ->paginate(5);

        //任务进度条设置
        foreach ($data as $k => $v) {
            $startTime = strtotime($v['create_time']);
            $allSecond = strtotime($v['deadline']) - $startTime;
            $usedSecond = time() - $startTime;
            if ($usedSecond > $allSecond) {
                $temp = 100;
            } else {
                //四舍五入取整
                $temp = round($usedSecond / $allSecond * 100);
            }
            $data[$k]['progress'] = $temp;
        }


        //分配数据
        $this->assign('data',$data);
        $this->assign('doing',$doing);
        $this->assign('done',$done);
        return view();
    }

















    //退出登录跳转到注册页面
    public function logout()
    {
        session('userLoginInfo',null);
        return redirect(url('Sign/in'));
    }


}
